package com.cheil.publish.service.ai;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.memory.redis.RedisChatMemoryRepository;
import com.cheil.publish.service.data.ChatsDto;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * @author ssk
 * @date 2025/9/5
 **/
@Component
@AllArgsConstructor
@Log4j2
public class DashscopeService {

    private final DashScopeChatModel dashScopeChatModel;
    private final RedisChatMemoryRepository redisChatMemoryRepository;
    private final DashscopeImageModel dashscopeImageModel;

    public Flux<String> chatStream(String prompt) {
        // 创建ChatMemory用于存储对话历史
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(redisChatMemoryRepository)
                // 保留最近10条消息
                .maxMessages(10)
                .build();
        // 创建ChatClient并添加MessageChatMemoryAdvisor
        MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory).build();
        ChatClient chatClient = ChatClient
                .builder(dashScopeChatModel)
                .defaultSystem("你是一个专业内容文案助手")
                .defaultAdvisors(messageChatMemoryAdvisor)
                .build();

        log.info("dashscope prompt: {}", prompt);
        return chatClient
                .prompt()
                .user(prompt)
                .stream()
                .content();
    }


    public ChatsDto chat(String prompt) {
        // 创建ChatMemory用于存储对话历史
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(redisChatMemoryRepository)
                // 保留最近10条消息
                .maxMessages(10)
                .build();
        // 创建ChatClient并添加MessageChatMemoryAdvisor
        MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory).build();
        ChatClient chatClient = ChatClient
                .builder(dashScopeChatModel)
                .defaultSystem("你是一个专业内容文案助手")
                .defaultAdvisors(messageChatMemoryAdvisor)
                .build();

        log.info("dashscope prompt: {}", prompt);
        return chatClient
                .prompt()
                .user(prompt)
                .call()
                .entity(ChatsDto.class);
    }


    /**
     * 根据文本生成图像
     * @param prompt 图像描述文本
     * @return ImageResponse 图像响应
     */
    public ImageResponse generateImage(String prompt) {
        log.info("Generating image with prompt: {}", prompt);
        ImagePrompt imagePrompt = new ImagePrompt(prompt);
        return dashscopeImageModel.call(imagePrompt);
    }

    /**
     * 根据文本生成图像（带选项）
     * @param prompt 图像描述文本
     * @param options 图像生成选项
     * @return ImageResponse 图像响应
     */
    public ImageResponse generateImage(String prompt, DashscopeImageOptions options) {
        log.info("Generating image with prompt: {} and options: {}", prompt, options);
        ImagePrompt imagePrompt = new ImagePrompt(prompt, options);
        return dashscopeImageModel.call(imagePrompt);
    }


}